package com.hevs.samplewebapp.server.entities;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hevs.samplewebapp.server.json.DateDeserializer;
import com.hevs.samplewebapp.server.json.DateSerializer;
import com.hevs.samplewebapp.server.json.KeyDeserializer;
import com.hevs.samplewebapp.server.json.KeySerializer;


@PersistenceCapable 
/**
 * This class is the Entity Medical Datas, it creates the table MedicalData in the Data Base.
 * 
 * @author Lionel Rey, Arnaud Durand
 * @date 08.03.12 
 */
public class MedicalRecord{
	
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	/**
	 * Key of the medical record
	 */
	private Key key;
	
	@Persistent
	/**
	 * Value of the medical record
	 */
	private double value;
	
	@Persistent
	/**
	 * Date of the medical record, inputed by the user
	 */
	private Date date;
	
	@Persistent
	/**
	 * Date of the medical record, saved on the server
	 */
	private Date timestamp;
	
	@Persistent
	/**
	 * Category of the medical record
	 */
	private int category;
	
	@Persistent
	/**
	 * Type of the medical record
	 */
	private int type;

	@Persistent
	/**
	 * Key of the owner of the medical record
	 */
	private Key user;
	
	// Constructors
	
	/**
	 * Constructor of medical record without parameters 
	 */
	public MedicalRecord()
	{
		
	}

	/**
	 * Constructor of medical record with parameters
	 * @param value
	 * value of the record
	 * @param date
	 * date of the record
	 * @param category
	 * category of the record
	 * @param type
	 * type of the record
	 * @param user
	 * owner of the record
	 */
	public MedicalRecord(double value, Date date, int category, int type, Key user){
		this.value = value;
		this.date = date;
		this.category = category;
		this.type = type;
		this.user=user;
	}

	
	// Getter and Setter
	/**
	 * Getter for the user
	 * @return
	 * the key of the user, owner of the record 
	 */
	@JsonSerialize(using = KeySerializer.class)
	public Key getUser() {
		return user;
	}
	
	/**
	 * Setter for the Key
	 * @param user
	 * key of the record's owner to set
	 */
	@JsonDeserialize(using = KeyDeserializer.class)
	public void setUser(Key user) {
		this.user = user;
	}
	
	/**
	 * Getter for the timestamp
	 * @return
	 * date of the record
	 */
	@JsonProperty
	public Date getTimestamp() {
		return timestamp;
	}
	
	/**
	 * Setter of the timestamp
	 * @param timestamp
	 * record's timestamp to set
	 */
	@JsonIgnore
	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	
	/**
	 * Getter for the Key
	 * @return
	 * the record's key
	 */
	@JsonSerialize(using = KeySerializer.class)
	public Key getKey() {
		return key;
	}
	
	/**
	 * Setter for the Key
	 * @param key
	 * record's key to set
	 */
	@JsonDeserialize(using = KeyDeserializer.class)
	public void setKey(Key key) {
		this.key = key;
	}
	
	@Deprecated
	@JsonIgnore
	public String getId(){
		if (this.key==null)
				return null;
		return KeyFactory.keyToString(this.key);
	}
	
	//only for the mock (no generated IDs)
	@Deprecated
	@JsonIgnore
	public void setId(String encoded){
		this.key=KeyFactory.stringToKey(encoded);
	}

	/**
	 * Getter for the value
	 * @return
	 * record's value
	 */
	public double getValue() {
		return value;
	}

	/**
	 * Setter for the value
	 * @param value
	 * record's value to set
	 */
	public void setValue(double value) {
		this.value = value;
	}
	
	/**
	 * Getter for the date
	 * @return
	 * record's date
	 */
	@JsonSerialize(using = DateSerializer.class)
	public Date getDate() {
		return date;
	}

	/**
	 * Setter for the date
	 * @param date
	 * record's date to set
	 */
	@JsonDeserialize(using = DateDeserializer.class)
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * Getter for the category
	 * @return
	 * record's category
	 */
	public int getCategory() {
		return category;
	}

	/**
	 * Setter for the category
	 * @param category
	 * record's category to set
	 */
	public void setCategory(int category) {
		this.category = category;
	}

	/**
	 * Getter for the type
	 * @return
	 * record's type
	 */
	public int getType() {
		return type;
	}

	/**
	 * Setter for the type
	 * @param type
	 * record's type to set
	 */
	public void setType(int type) {
		this.type = type;
	}


	
}
